#include "dwgcircle2.h"
#include <QtMath>

DwgCircle2::DwgCircle2(QObject *parent)
    : DwgObject{parent},
      mRad(0)
{
}

void DwgCircle2::SetData(QPointF pt1, QPointF pt2)
{
    // 圆心为两点中点
    mCenPt = QPointF((pt1.x() + pt2.x()) / 2.0, (pt1.y() + pt2.y()) / 2.0);
    // 半径为两点距离的一半
    mRad = qSqrt(qPow(pt2.x() - pt1.x(), 2) + qPow(pt2.y() - pt1.y(), 2)) / 2.0;
}

void DwgCircle2::Draw(QPainter *painter)
{
    //=========抑制线宽变化========//
    QPen pen;
    pen.setCosmetic(true);       // 允许线宽缩放
    pen.setWidth(1);             // 使用逻辑线宽
    painter->setPen(pen);
    //==========选择+删除=========//
    if(mDeleteed) return;
    if(mSelected)
    {
        painter->setPen(Qt::red);
    }

    painter->drawEllipse(mCenPt, mRad, mRad);
    painter->drawLine(mCenPt.x()-2,mCenPt.y(),mCenPt.x()+2,mCenPt.y());
    painter->drawLine(mCenPt.x(),mCenPt.y()-2,mCenPt.x(),mCenPt.y()+2);

    painter->setPen(Qt::black);
}

//==选择+删除==//
void DwgCircle2::Select(QPointF pt)
{
    qreal dist = sqrt((pt.x()-mCenPt.x())*(pt.x()-mCenPt.x())
                      +(pt.y()-mCenPt.y())*(pt.y()-mCenPt.y()));
    if(fabs(dist-mRad) < 3)
        mSelected = true;
}
